package com.woniuxy.mqconsumer.consumer;

import com.rabbitmq.client.Channel;
import com.woniuxy.entity.Student;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * description: MyConsumer <br>
 * date: 2022/1/12 12:49 下午 <br>
 * author: shesaifei <br>
 * version: 1.0 <br>
 */

/**
 * 定义一个bean，消费者，监听队列，定义处理消费者的方法
 */
@Component
public class MyConsumer {

    //指定当前方法监听哪个队列
    @RabbitListener(queues = "queueemail")
    //String msg,   消息体
    //Message message 消息对象
    // com.rabbitmq.client.Channel channel  信道

    public void receiveEmail(String msg, Message message, Channel channel) throws IOException {
        System.out.println("处理Email");
        System.out.println("msg:" + msg);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("h1:" + message.getMessageProperties().getHeaders().get("h1"));
        System.out.println("h1:" + message.getMessageProperties().getHeaders().get("h2"));
        System.out.println("-----------------");
    }

    @RabbitListener(queues = "queuesms")
    public void receiveSms(Student msg, Message message, Channel channel) throws IOException, InterruptedException {
        //Thread.sleep(10000);
        System.out.println("处理SmS");
        System.out.println("msg:" + msg);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        //channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("-----------------");
    }

    //监听死信队列
    @RabbitListener(queues = "queueorderdlx")
    public void receiveOrderdlx(Student msg, Message message, Channel channel) throws IOException {
        System.out.println("10秒中关闭订单状态：" + new Date());
        System.out.println("msg:" + msg);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("---------------------");
    }

    //监听延迟队列

    @RabbitListener(queues = "queuedelay")
    public void receiveDelay(Student msg, Message message, Channel channel) throws IOException {
        System.out.println("处理延迟队列：" + new Date());
        System.out.println("msg:" + msg);
        System.out.println("message:" + message);
        System.out.println("channel:" + channel);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        System.out.println("---------------------");
    }
}
